package com.practicesuite.controllayer;

import java.awt.EventQueue;
import java.util.List;

import javax.swing.JFrame;

import com.practicesuite.systemlayer.Appointment;
import com.practicesuite.systemlayer.Invoice;
import com.practicesuite.systemlayer.Patient;
import com.practicesuite.systemlayer.TreatmentRecord;
import com.practicesuite.systemlayer.User;

/**
 * Main class for running user interface, creates frame and populates frame 
 * with different panels from control layer.
 * 
 * @author James
 *
 */
public class MainWindow {

	public final JFrame frame;
	private AdminHomePanel adminHomePanel;
    private AppointmentListPanel appointmentListPanel;
    private CreateDoctorsOrdersPanel createDoctorsOrdersPanel;
    private CreatePatientPanel createPatientPanel;
    private CreateTreatmentPanel createTreatmentPanel;
    private CreateUserPanel createUserPanel;
    private DoctorHomePanel doctorHomePanel;
    private EditPatientPanel editPatientPanel;
    private EditUserPanel editUserPanel;
	private LoginPanel loginPanel;
    private NurseHomePanel nurseHomePanel;
    private PatientAdministrationPanel patientAdministrationPanel;
    private PatientHomePanel patientHomePanel;
	private RegistrationPanel registrationPanel;
	private RequestAppointmentPanel requestAppointmentPanel;
	private TreatmentListPanel treatmentListPanel;
	private UserAdministrationPanel userAdministrationPanel;
	private ViewAppointmentPanel viewAppointmentPanel;
	private ViewInvoicePanel viewInvoicePanel;
	private ViewMedicalHistoryPanel viewMedicalHistoryPanel;
	private ViewStatisticsPanel viewStatisticsPanel;
	private ViewTreatmentPanel viewTreatmentPanel;
	
	public MainWindow() {
		frame = new JFrame("Doctor's Office Software");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(300, 200, 600, 400);
        
		loginPanel = new LoginPanel(this);
		frame.getContentPane().add(loginPanel);
	}
	
	/**
	 * Changes the current panel for the main frame to a generic panel with no 
	 * current user information.
	 * 
	 * @param destPanel the name of the panel to go to
	 */
	public void gotoPanel(String destPanel) {
        frame.getContentPane().invalidate();
        frame.getContentPane().removeAll();
        if (destPanel == "login") {
            loginPanel = new LoginPanel(this);
            frame.getContentPane().add(loginPanel);
        }
        else if (destPanel == "registration") {
            registrationPanel = new RegistrationPanel(this);
            frame.getContentPane().add(registrationPanel);
        }
        else {
            System.out.println("Panel type " + destPanel + " is not identified.");
        }
        frame.getContentPane().validate();
	}
	
	/**
	 * Changes the current panel to a panel where the user is currently logged 
	 * in.
	 * 
	 * @param destPanel the name of the panel to go to
	 * @param user the user who is currently logged in
	 */
	public void gotoPanel(String destPanel, User user) {
        frame.getContentPane().invalidate();
        frame.getContentPane().removeAll();
        if (destPanel == "adminHome") {
            adminHomePanel = new AdminHomePanel(this, user);
            frame.getContentPane().add(adminHomePanel);
        }
        else if (destPanel == "appointmentList") {
            appointmentListPanel = new AppointmentListPanel(this, user);
            frame.getContentPane().add(appointmentListPanel);
        }
        else if (destPanel == "createDoctorsOrders") {
            try {
                createDoctorsOrdersPanel = new CreateDoctorsOrdersPanel(this, user);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            frame.getContentPane().add(createDoctorsOrdersPanel);
        }
        else if (destPanel == "createPatient") {
            createPatientPanel = new CreatePatientPanel(this, user);
            frame.getContentPane().add(createPatientPanel);
        }
        else if (destPanel == "createTreatment") {
            try {
                createTreatmentPanel = new CreateTreatmentPanel(this, user);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            frame.getContentPane().add(createTreatmentPanel);
        }
        else if (destPanel == "createUser") {
            createUserPanel = new CreateUserPanel(this, user);
            frame.getContentPane().add(createUserPanel);
        }
        else if (destPanel == "doctorHome") {
            doctorHomePanel = new DoctorHomePanel(this, user);
            frame.getContentPane().add(doctorHomePanel);
        }
        else if (destPanel == "nurseHome") {
            nurseHomePanel = new NurseHomePanel(this, user);
            frame.getContentPane().add(nurseHomePanel);
        }
        else if (destPanel == "patientHome") {
            patientHomePanel = new PatientHomePanel(this, user);
            frame.getContentPane().add(patientHomePanel);
        }
        else if (destPanel == "requestAppointment") {
            requestAppointmentPanel = new RequestAppointmentPanel(this, user);
            frame.getContentPane().add(requestAppointmentPanel);
        }
        else if (destPanel == "treatmentList") {
            treatmentListPanel = new TreatmentListPanel(this, user);
            frame.getContentPane().add(treatmentListPanel);
        }
        else if (destPanel == "viewMedicalHistory") {
            viewMedicalHistoryPanel = new ViewMedicalHistoryPanel(this, null, user);
            frame.getContentPane().add(viewMedicalHistoryPanel);
        }
        else if (destPanel == "viewStatistics") {
            viewStatisticsPanel = new ViewStatisticsPanel(this, null, user);
            frame.getContentPane().add(viewStatisticsPanel);
        }
        else {
            System.out.println("Panel type " + destPanel + " is not identified.");
        }
        frame.getContentPane().validate();
	}
	
	/**
	 * Changes the current panel to a panel in which a user's information is 
	 * being edited.
	 * 
	 * @param destPanel the name of the panel to go to
	 * @param targetUser the user whose information is being edited
	 * @param user the user who is currently logged in
	 */
	public void gotoPanel(String destPanel, User targetUser, User user) {
        frame.getContentPane().invalidate();
        frame.getContentPane().removeAll();
        if (destPanel == "editPatient") {
            editPatientPanel = new EditPatientPanel(this, targetUser, user);
            frame.getContentPane().add(editPatientPanel);
        }
        else if (destPanel == "editUser") {
            editUserPanel = new EditUserPanel(this, targetUser, user);
            frame.getContentPane().add(editUserPanel);
        }
        else {
            System.out.println("Panel type " + destPanel + " is not identified.");
        }
        frame.getContentPane().validate();
    }
	
	/**
	 * Changes the current panel to a panel in which appointment details are 
	 * being viewed.
	 * 
	 * @param destPanel the name of the panel to go to
	 * @param appointment the appointment being viewed
	 * @param user the user who is currently logged in
	 */
	public void gotoPanel(String destPanel, Appointment appointment, User user) {
        frame.getContentPane().invalidate();
        frame.getContentPane().removeAll();
        if (destPanel == "viewAppointment") {
            viewAppointmentPanel = new ViewAppointmentPanel(this, appointment, user);
            frame.getContentPane().add(viewAppointmentPanel);
        }
        else {
            System.out.println("Panel type " + destPanel + " is not identified.");
        }
        frame.getContentPane().validate();
    }
	
	/**
	 * Changes the current panel to a panel in which a treatment record's 
	 * details are being viewed.
	 * 
	 * @param destPanel the name of the panel to go to
	 * @param treatment the treatment record being viewed
	 * @param user the user who is currently logged in
	 */
	public void gotoPanel(String destPanel, TreatmentRecord treatment, User user) {
        frame.getContentPane().invalidate();
        frame.getContentPane().removeAll();
        if (destPanel == "viewTreatment") {
            viewTreatmentPanel = new ViewTreatmentPanel(this, treatment, user);
            frame.getContentPane().add(viewTreatmentPanel);
        }
        else {
            System.out.println("Panel type " + destPanel + " is not identified.");
        }
        frame.getContentPane().validate();
    }
	
	public void gotoPanel(String destPanel, Invoice invoice, User user) {
	    frame.getContentPane().invalidate();
        frame.getContentPane().removeAll();
        if (destPanel == "viewInvoice") {
            viewInvoicePanel = new ViewInvoicePanel(this, invoice, user);
            frame.getContentPane().add(viewInvoicePanel);
        }
        else {
            System.out.println("Panel type " + destPanel + " is not identified.");
        }
        frame.getContentPane().validate();
	}
	
	/**
	 * Changes the current panel to a panel in which a list of patients is 
	 * being viewed.
	 * 
	 * @param destPanel the name of the panel to go to
	 * @param users the patients in the list
	 * @param user the user who is currently logged in
	 */
	public void gotoPanelPatients(String destPanel, List<Patient> users, User user) {
	    frame.getContentPane().invalidate();
	    frame.getContentPane().removeAll();
	    if (destPanel == "patientAdministration") {
	        patientAdministrationPanel = new PatientAdministrationPanel(this, users, user);
	        frame.getContentPane().add(patientAdministrationPanel);
	    }
	    else {
	        System.out.println("Panel type " + destPanel + " is not identified.");
	    }
	    frame.getContentPane().validate();
	}
    
	/**
	 * Changes the current panel to a panel in which a list of users is being 
	 * viewed.
     * 
	 * @param destPanel the name of the panel to go to
	 * @param users the users in the list
	 * @param user the user who is currently logged in
	 */
    public void gotoPanelUsers(String destPanel, List<User> users, User user) {
        frame.getContentPane().invalidate();
        frame.getContentPane().removeAll();
        if (destPanel == "userAdministration") {
            userAdministrationPanel = new UserAdministrationPanel(this, users, user);
            frame.getContentPane().add(userAdministrationPanel);
        }
        else {
            System.out.println("Panel type " + destPanel + " is not identified.");
        }
        frame.getContentPane().validate();
    }
	
    /**
     * Refreshes a panel to display information for a selected patient or date.
     * 
     * @param currentPanel the name of the current panel
     * @param patient the patient whose information is being viewed
     * @param date the date from which information is being viewed
     * @param user the user who is currently logged in
     */
	public void refresh(String currentPanel, Patient patient, int[] date, User user) {
	    frame.getContentPane().invalidate();
	    frame.getContentPane().removeAll();
	    if (currentPanel == "viewStatistics") {
	        viewStatisticsPanel = new ViewStatisticsPanel(this, date, user);
	        frame.getContentPane().add(viewStatisticsPanel);
	    }
	    else if (currentPanel == "viewMedicalHistory") {
	        viewMedicalHistoryPanel = new ViewMedicalHistoryPanel(this, patient, user);
	        frame.getContentPane().add(viewMedicalHistoryPanel);
	    }
        else {
            System.out.println("Panel type " + currentPanel + " is not identified.");
        }
	    frame.getContentPane().validate();
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow mainWindow = new MainWindow();
					mainWindow.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

}
